<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Send a CUBRID query</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.cubrid-ping.html">« cubrid_ping</a></li>
      <li style="float: right;"><a href="function.cubrid-real-escape-string.html">cubrid_real_escape_string »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="cubridmysql.cubrid.html">CUBRID MySQL 兼容性函数</a></li>
    <li>Send a CUBRID query</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.cubrid-query" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">cubrid_query</h1>
  <p class="verinfo">(PECL CUBRID &gt;= 8.3.1)</p><p class="refpurpose"><span class="refname">cubrid_query</span> &mdash; <span class="dc-title">Send a CUBRID query</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.cubrid-query-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>cubrid_query</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$query</code></span>, <span class="methodparam"><span class="type">resource</span> <code class="parameter">$conn_identifier</code><span class="initializer"> = ?</span></span>): <span class="type">resource</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>cubrid_query()</strong></span> sends a unique query (multiple queries are not supported) to the 
   currently active database on the server that&#039;s associated with the specified <code class="parameter">conn_identifier</code>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.cubrid-query-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">query</code></dt>

     <dd>

      <p class="para">
       An SQL query
      </p>
      <p class="para">
       Data inside the query should be <a href="function.cubrid-real-escape-string.html" class="link">properly escaped</a>.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">conn_identifier</code></dt>

     <dd>

      <p class="para">
       The CUBRID connection. If the connection identifier is not specified,
       the last connection opened by <span class="function"><a href="function.cubrid-connect.html" class="function">cubrid_connect()</a></span> is
       assumed.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.cubrid-query-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset,
   <span class="function"><strong>cubrid_query()</strong></span> returns a <span class="type">resource</span> on success, or <strong><code>false</code></strong> on error.
  </p>
  <p class="para">
   For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc,
   <span class="function"><strong>cubrid_query()</strong></span> returns <strong><code>true</code></strong> on success or <strong><code>false</code></strong> on error.
  </p>
  <p class="para">
   The returned result resource should be passed to <span class="function"><a href="function.cubrid-fetch-array.html" class="function">cubrid_fetch_array()</a></span>, and other
   functions for dealing with result tables, to access the returned data.
  </p>
  <p class="para">
   Use <span class="function"><a href="function.cubrid-num-rows.html" class="function">cubrid_num_rows()</a></span> to find out how many rows were returned for a SELECT statement or
   <span class="function"><a href="function.cubrid-affected-rows.html" class="function">cubrid_affected_rows()</a></span> to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.
  </p>
  <p class="para">
   <span class="function"><strong>cubrid_query()</strong></span> will also fail and return <strong><code>false</code></strong>
   if the user does not have permission to access the table(s) referenced by the query.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.cubrid-query-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-1203">
    <p><strong>示例 #1 Invalid Query</strong></p>
    <div class="example-contents"><p>
     The following query is syntactically invalid, so <span class="function"><strong>cubrid_query()</strong></span> fails and returns <strong><code>false</code></strong>.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">cubrid_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'localhost'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">33000</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'demodb'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">cubrid_query</span><span style="color: #007700">(</span><span style="color: #DD0000">'SELECT&nbsp;*&nbsp;WHERE&nbsp;1=1'</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$result</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">'Invalid&nbsp;query:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">cubrid_error</span><span style="color: #007700">());<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-1204">
    <p><strong>示例 #2 Valid Query</strong></p>
    <div class="example-contents"><p>
     The following query is valid, so <span class="function"><strong>cubrid_query()</strong></span> returns a <span class="type">resource</span>.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;This&nbsp;could&nbsp;be&nbsp;supplied&nbsp;by&nbsp;a&nbsp;user,&nbsp;for&nbsp;example<br /></span><span style="color: #0000BB">$firstname&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'fred'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$lastname&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'fox'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">cubrid_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'localhost'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">33000</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'demodb'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">cubrid_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,</span><span style="color: #DD0000">"DROP&nbsp;TABLE&nbsp;if&nbsp;exists&nbsp;friends"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">cubrid_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,</span><span style="color: #DD0000">"create&nbsp;table&nbsp;friends(firstname&nbsp;varchar,lastname&nbsp;varchar,address&nbsp;char(24),age&nbsp;int)"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">cubrid_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,</span><span style="color: #DD0000">"insert&nbsp;into&nbsp;friends&nbsp;values('fred','fox','home-1','20')"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">cubrid_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,</span><span style="color: #DD0000">"insert&nbsp;into&nbsp;friends&nbsp;values('blue','cat','home-2','21')"</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;Formulate&nbsp;Query<br />//&nbsp;This&nbsp;is&nbsp;the&nbsp;best&nbsp;way&nbsp;to&nbsp;perform&nbsp;an&nbsp;SQL&nbsp;query<br />//&nbsp;For&nbsp;more&nbsp;examples,&nbsp;see&nbsp;cubrid_real_escape_string()<br /></span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT&nbsp;firstname,&nbsp;lastname,&nbsp;address,&nbsp;age&nbsp;FROM&nbsp;friends&nbsp;WHERE&nbsp;firstname='%s'&nbsp;AND&nbsp;lastname='%s'"</span><span style="color: #007700">,<br /></span><span style="color: #0000BB">cubrid_real_escape_string</span><span style="color: #007700">(</span><span style="color: #0000BB">$firstname</span><span style="color: #007700">),<br /></span><span style="color: #0000BB">cubrid_real_escape_string</span><span style="color: #007700">(</span><span style="color: #0000BB">$lastname</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">//&nbsp;Perform&nbsp;Query<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">cubrid_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$query</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Check&nbsp;result<br />//&nbsp;This&nbsp;shows&nbsp;the&nbsp;actual&nbsp;query&nbsp;sent&nbsp;to&nbsp;CUBRID,&nbsp;and&nbsp;the&nbsp;error.&nbsp;Useful&nbsp;for&nbsp;debugging.<br /></span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">$result</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$message&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Invalid&nbsp;query:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">cubrid_error</span><span style="color: #007700">()&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$message&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">'Whole&nbsp;query:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$query</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #0000BB">$message</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;Use&nbsp;result<br />//&nbsp;Attempting&nbsp;to&nbsp;print&nbsp;$result&nbsp;won't&nbsp;allow&nbsp;access&nbsp;to&nbsp;information&nbsp;in&nbsp;the&nbsp;resource<br />//&nbsp;One&nbsp;of&nbsp;the&nbsp;cubrid&nbsp;result&nbsp;functions&nbsp;must&nbsp;be&nbsp;used<br />//&nbsp;See&nbsp;also&nbsp;cubrid_result(),&nbsp;cubrid_fetch_array(),&nbsp;cubrid_fetch_row(),&nbsp;etc.<br /></span><span style="color: #007700">while&nbsp;(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">cubrid_fetch_assoc</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'firstname'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'lastname'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'address'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'age'</span><span style="color: #007700">];<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;Free&nbsp;the&nbsp;resources&nbsp;associated&nbsp;with&nbsp;the&nbsp;result&nbsp;set<br />//&nbsp;This&nbsp;is&nbsp;done&nbsp;automatically&nbsp;at&nbsp;the&nbsp;end&nbsp;of&nbsp;the&nbsp;script<br /></span><span style="color: #0000BB">cubrid_free_result</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.cubrid-query-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.cubrid-connect.html" class="function" rel="rdfs-seeAlso">cubrid_connect()</a> - Open a connection to a CUBRID Server</span></li>
    <li class="member"><span class="function"><a href="function.cubrid-error.html" class="function" rel="rdfs-seeAlso">cubrid_error()</a> - Get the error message</span></li>
    <li class="member"><span class="function"><a href="function.cubrid-real-escape-string.html" class="function" rel="rdfs-seeAlso">cubrid_real_escape_string()</a> - Escape special characters in a string for use in an SQL statement</span></li>
    <li class="member"><span class="function"><a href="function.cubrid-result.html" class="function" rel="rdfs-seeAlso">cubrid_result()</a> - Return the value of a specific field in a specific row</span></li>
    <li class="member"><span class="function"><a href="function.cubrid-fetch-assoc.html" class="function" rel="rdfs-seeAlso">cubrid_fetch_assoc()</a> - Return the associative array that corresponds to the fetched row</span></li>
    <li class="member"><span class="function"><a href="function.cubrid-unbuffered-query.html" class="function" rel="rdfs-seeAlso">cubrid_unbuffered_query()</a> - Perform a query without fetching the results into memory</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>